﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CubSeeMusicCtrl: MonoBehaviour
{
        [Header("音频解码器")]
        public CubSeeMusicDecode smd;

        [Header("高音颜色")]
        public Color hColor = Color.red;
        [Header("高音分界线")]
        public float hVal = 0.1f;
        [Header("中音颜色")]
        public Color mColor = Color.green;
        [Header("低音分界线")]
        public float lVal = 0.08f;
        [Header("低音颜色")]
        public Color lColor = Color.blue;
        [Header("材质")]
        public Material hdrMaterial;

        public Transform[] cylinders;

        public List<Material> mat = new List<Material>();

        void Start()
        {
                //获取所有子对象
                GameObject[] objs = new GameObject[this.transform.childCount];
                for (int i = 0; i < this.transform.childCount; i++)
                {
                        objs[i] = this.transform.GetChild(i).gameObject;
                        Debug.Log(transform.GetChild(i).name);
                }

                cylinders = new Transform[objs.Length];
                for (int i=0;i<objs.Length;i++)
                {
                        cylinders[i] = objs[i].transform;
                }

                if (!smd)
                {
                        smd = this.GetComponent<CubSeeMusicDecode>();
                }

                //第一个是自己
                for (int i = 0; i < cylinders.Length; i++)
                {
                        mat.Add(new Material(hdrMaterial));
                        cylinders[i].localPosition = new Vector3((float)i * 1f, 0f, 0f);
                        cylinders[i].GetComponent<MeshRenderer>().material = mat[i];
                }
        }


        void Update()
        {
                if (smd)
                {
                        float tagY = cylinders[0].localScale.y;
                        for (int i = 0; i < cylinders.Length; i++)
                        {
                                if (smd.GetSpectrum(i) * 10 >= hVal)
                                {
                                        cylinders[i].GetComponent<MeshRenderer>().material.SetColor("_EmissionColor", hColor);
                                }
                                else if (smd.GetSpectrum(i) * 10 <= hVal && smd.GetSpectrum(i) * 10 >= lVal)
                                {
                                        cylinders[i].GetComponent<MeshRenderer>().material.SetColor("_EmissionColor", mColor);
                                }
                                else if (smd.GetSpectrum(i) * 10 <= lVal)
                                {
                                        cylinders[i].GetComponent<MeshRenderer>().material.SetColor("_EmissionColor", lColor);
                                }

                                cylinders[i].localScale = new Vector3(cylinders[i].localScale.x, scaleDefault + smd.GetSpectrum(i) * scaleShow, cylinders[i].localScale.z);
                        }
                }
        }
        [Header("默认缩放")]
        public float scaleDefault = 0.2f;
        [Header("缩放系数")]
        public float scaleShow = 27;
}
